package gant.tools

/**
 * Adds some classpath helper utilities
 */
class ClassUtils {

    /**
     * should never implement since this is a utility
     */
    private ClassUtils() {}

    /**
     * stolen from Spring's org.springframework.util.ClassUtils
     *
     * @return default classloader to load classes
     */
    public static ClassLoader getDefaultClassLoader() {
        ClassLoader cl = null;
        try {
            cl = Thread.currentThread().getContextClassLoader();
        }
        catch (Throwable ex) {
            // Cannot access thread context ClassLoader - falling back to system class loader...
        }
        if (cl == null) {
            // No thread context class loader -> use class loader of this class.
            cl = ClassUtils.class.getClassLoader();
        }
        return cl;
    }

    /**
     * improved forName that use the {@link ClassUtils#getDefaultClassLoader}
     * @param className
     * @return the class
     */
    public static Class forName(String className) {
        getDefaultClassLoader().loadClass(className)
    }
}
